/*
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
import Want from '@ohos.app.ability.Want';
import Log from '../../../../../../common/src/main/ets/default/Log';
import AbilityManager from '../../../../../../common/src/main/ets/default/abilitymanager/abilityManager';
import initSystemUi from '../../../../../../common/src/main/ets/default/InitSystemUi';
import inputConsumer from '@ohos.multimodalInput.inputConsumer';
import promptAction from '@ohos.promptAction';
import { KeyCode } from '@ohos.multimodalInput.keyCode';
import audio from '@ohos.multimedia.audio';
import { audioManager } from '@ohos/common/src/main/ets/util/GetAudioManager';

const TAG = 'SystemUI_ServiceExtAbility';

//ServiceExtensionAbility是一种专门用于后台服务的ExtensionAbility类型，它不能直接显示在界面上，只能被拉起运行。
class ServiceExtAbility extends ServiceExtensionAbility {
  onCreate(want: Want): void {
    AppStorage.setOrCreate('OUTPUT_STATE', audio.DeviceType.SPEAKER)
    Log.showInfo(TAG, `onCreate, want: ${JSON.stringify(want)}`);
    initSystemUi(this.context);
    audioManager.init()
    // 'SystemUi_Entry_Context对应context'
    AbilityManager.setContext(AbilityManager.ABILITY_NAME_ENTRY, this.context);
    // 监听音量加减
    audioManager.audioVolumeManager.on('volumeChange', async (volumeEvent: audio.VolumeEvent) => {
      const devices = await audioManager.audioRoutingManager.getDevices(audio.DeviceFlag.OUTPUT_DEVICES_FLAG)
      /*  promptAction.showDialog({ message: JSON.stringify(devices) })*/
      //AppStorage.set<number>('OUTPUT_STATE', devices.deviceType)
      AppStorage.set<number>('OUTPUT_STATE', devices[0].deviceType)
      /* promptAction.showToast({
         message: `VolumeType of stream: ${volumeEvent.volumeType} ` + `Volume level: ${volumeEvent.volume} ` +
           `Whether to updateUI: ${volumeEvent.updateUi} `
       })*/
      Log.showInfo(TAG, `输出设备类型: ${devices[0].deviceType} ` + `当前音量: ${volumeEvent.volume} `);
    });
    // 监听遥控器按键事件
    /*    inputConsumer.on("key", {
          preKeys: [],
          finalKey: KeyCode.KEYCODE_VOLUME_UP,
          isFinalKeyDown: true,
          finalKeyDownDuration: 0
        }, (t) => {
          Log.showInfo(TAG, `音量键+: ${JSON.stringify(t)}`);
          promptAction.showToast({ message: '音量键+' })
          */
    /*NavigationBarEventModel.distinctionKeyCode(t);*/
    /*
       })*/

    /* inputConsumer.on("key", {
       preKeys: [],
       finalKey: KeyCode.KEYCODE_VOLUME_DOWN,
       isFinalKeyDown: true,
       finalKeyDownDuration: 100
     }, (t) => {
       Log.showInfo(TAG, `音量键-: ${JSON.stringify(t)}`);
       promptAction.showToast({ message: '音量键-' })
       */
    /*NavigationBarEventModel.distinctionKeyCode(t);*/
    /*
       })*/

    /*inputConsumer.on("key", {
      preKeys: [],
      finalKey: KeyCode.KEYCODE_BACK,
      isFinalKeyDown: true,
      finalKeyDownDuration: 0
    }, (t) => {
      Log.showInfo(TAG, `返回: ${JSON.stringify(t)}`);
      promptAction.showToast({ message: '返回' })

      */
    /*NavigationBarEventModel.distinctionKeyCode(t);*/
    /*
       })*/
  }

  onDestroy(): void {
    Log.showInfo(TAG, 'onDestroy');
  }
}

export default ServiceExtAbility;